package org.takeback.chat.schedule;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.takeback.chat.entity.GcLottery;
import org.takeback.chat.entity.Message;
import org.takeback.chat.lottery.listeners.GameException;
import org.takeback.chat.lottery.listeners.RoomAndLotteryListener;
import org.takeback.chat.service.LHHNumDetailService;
import org.takeback.chat.service.LHHPlanService;
import org.takeback.chat.service.LotteryService;
import org.takeback.chat.service.RoomService;
import org.takeback.chat.service.support.RedMessageProcessor;
import org.takeback.chat.store.room.LotteryFactory;
import org.takeback.chat.store.room.Room;
import org.takeback.chat.store.room.RoomStore;
import org.takeback.chat.store.user.User;
import org.takeback.chat.store.user.UserStore;
import org.takeback.chat.utils.DateUtil;
import org.takeback.chat.utils.MessageUtils;
import org.takeback.chat.utils.RedEnvelopesUtils;
import org.takeback.service.BaseService;
import org.takeback.util.BeanUtils;
@Service
public class LHHSchedule extends BaseService{
	public static final Logger LOGGER = LoggerFactory.getLogger(LHHSchedule.class);
	
	@Autowired
	private RedMessageProcessor redMessageProcessor;
	
	@Autowired
	private LotteryService lotteryService;
	@Autowired
	RoomStore roomStore;
	@Autowired
	UserStore userStore;
	
	@Autowired
	private RoomService roomService;
	
	@Autowired
	private LHHPlanService lHHPlanService;
	
	@Autowired
	LHHNumDetailService lhhNumDetailService;
	
	public void GO7AutoSendLettery() {
		System.out.println("进来了"+ DateUtil.toTime(new Date()));
		//更新上次开奖时间
		roomService.updateLHHConfig(DateUtil.toTime(new Date()), "conf_last_time_task_time");
		Room room =  this.roomStore.get("LHH0001");
		//1停止押注
		roomService.updateLHHConfig("1", "config_lhl_stop_or_start");
		RedEnvelopesUtils relu = lHHPlanService.getLHHPlan();
		Message msg = new Message();
		msg.setContent("结束押注，等待开奖！");
		msg.setType(Message.TXT_SYS);
		MessageUtils.broadcast(room, msg);
		
		User user = this.userStore.get(438);
		Message message = new Message();
		LotteryFactory.DefaultLotteryBuilder builder = generateLottery(message, room, user);
		if (builder == null) {
			return;
		}
		Map<String, List<Double>> redRandom = new HashMap<String, List<Double>>();
		List<Double> a = relu.getListLast();
		Collections.reverse(a);
		redRandom.put(builder.getLotteryId(), a);
		builder.setRedRadom(redRandom);
		
		Map<String, List<Integer>> redRandomLast = new HashMap<String, List<Integer>>();
		redRandomLast.put(builder.getLotteryId(), relu.getListRedMoney());
		builder.setRedRadomLast(redRandomLast);
		RoomAndLotteryListener listener = room.getRoomAndLotteryListener();
//		if (listener != null) {
//			try {
//				if (!listener.onBeforeRed(builder)) {
//					return;
//				}
//			} catch (GameException e) {
//				LOGGER.error(e.getMessage());
////				MessageUtils.sendCMD(session, "alert", e.getMessage());
//				return;
//			}
//		}
		builder.setRoom(room);
		GcLottery gcLottery = new GcLottery();
        gcLottery.setId(builder.getLotteryId());
        gcLottery.setRoomId(builder.getRoomId());
        gcLottery.setCreateTime(new Date());
        gcLottery.setDescription(builder.getDescription());
        gcLottery.setMoney(builder.getMoney());
        gcLottery.setSender(builder.getSender());
        gcLottery.setNumber(builder.getNumber());
        gcLottery.setStatus("0");
        gcLottery.setType(builder.getType());
        gcLottery.setExpiredSeconds(40);
        this.lotteryService.save(GcLottery.class, gcLottery);
        final Message redMessage = BeanUtils.map(message, Message.class);
        redMessage.setContent(gcLottery);
        redMessage.setCmd(Message.RED);
        redMessage.setType(Message.RED);
        redMessage.setHeadImg(user.getHeadImg());
        MessageUtils.broadcast(room, redMessage);
		if (listener != null) {
			try {
				listener.onRed(builder);
			} catch (GameException e) {
				LOGGER.error(e.getMessage());
//				MessageUtils.broadcast("alert", e.getMessage());
			}
		}
		
	}
	
	protected LotteryFactory.DefaultLotteryBuilder generateLottery(Message message, Room room,
			User user) {
		try {
			BigDecimal money = new BigDecimal(5);
			Integer number = 5;
			Integer no = lhhNumDetailService.getLhhNum();
            final LotteryFactory.DefaultLotteryBuilder builder = LotteryFactory.getDefaultBuilder(money, number).setType("2").setExpiredSeconds(40).setSender(user.getId()).setRoomId(room.getId()).setDescription("第" + (no+1) + "开奖红包");
            
            return builder;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCmd("");
			message.setSender(user.getId());
			message.setContent("开奖失败");
			MessageUtils.broadcast(room, message);
		}
		return null;
	}
	
//	public RedEnvelopesUtils computed() {
//		//获取上次开奖时间
//		Map<String, Object> p = roomService.getRoomProperties("LHH0001");
//		String lastOpenDateTimeStr = p.get("conf_last_time_task_time").toString();
//		System.out.println(lastOpenDateTimeStr);
//		Date lastTime = DateUtil.toDate(lastOpenDateTimeStr);
//		
//		RedEnvelopesUtils relu = lHHPlanService.diguiHb(llList, p);
//		return relu;
//	}
}
